
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:h_flutter_119/utils/event_bus.dart';
import 'package:h_flutter_119/utils/log_utils.dart';

class TestEventBusPage extends StatelessWidget{

  @override
  Widget build(BuildContext context) {
    return Container(
      child: _buildWidget,
    );
  }

  Subscription? subscription;
  void registerEventBus(){
    var bus1 = EventBus();
    var bus2 = EventBus();
    LogD('bus1=>${bus1.hashCode} bus2=>${bus2.hashCode} subscription=${subscription.hashCode}');

    subscription = bus.on('test', (arg) { 
      LogD('receiveMsg=>$arg');
      Fluttertoast.showToast(msg: arg.toString());
    });
  }

  void unregisterEventBus(){
    bus.off('test',subscription);
  }

  void emitEvent(){
    bus.emit('test', 'hahhahahahahah');
  }

  Widget get _buildWidget{
    return Column(
      children: [
        ElevatedButton(onPressed: () => registerEventBus(), child: Text('register')),
        ElevatedButton(onPressed: () => unregisterEventBus(), child: Text('unregister')),
        ElevatedButton(onPressed: () => emitEvent(), child: Text('emit')),
      ],
    );
  }


}
